//
//  AppDelegate.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/10/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "AppDelegate.h"
#import "ZDTabBarController.h"
#import "ZDWelcomeViewController.h"
#import "ZDNewFeatureViewController.h"
#import "ZDUserAccount.h"

NSString *const ZDSwitchControllerNotification = @"ZDSwitchControllerNotification";

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = [self defaultController];
    
    [self.window makeKeyAndVisible];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchController:) name:ZDSwitchControllerNotification object:nil];
    
    return YES;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 显示控制器
- (UIViewController *)defaultController
{
    //是否登录
    if ([ZDUserAccount isLogin])
    {
        //登录了，是否新版本
        if ([self isNewVersion])
        {
            //新版本 显示新特性控制器
            return [[ZDNewFeatureViewController alloc] init];
        }
        else
        {
            //不是新版本 显示欢迎控制器
            return [[ZDWelcomeViewController alloc] init];
        }
    }
    else
    {
        //没有登录, 如果是新版本, 显示新版本控制器(第一次下载打开); 没有登录, 不是新版本, 显示主要控制器
        return [self isNewVersion] ? [[ZDNewFeatureViewController alloc] init] : [[ZDTabBarController alloc] init];
    }
}

#pragma mark - 判断是否新版本
- (BOOL)isNewVersion
{
    //1.获取当前版本号
    NSString *currentVersionStr = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    double currentVersion = currentVersionStr.doubleValue;
    
    //2.获取沙盒保存的版本号
    double sandBoxVersion = [[NSUserDefaults standardUserDefaults] doubleForKey:@"sandBoxVersion"];
    
    //3.比较
    if (currentVersion > sandBoxVersion)
    {
        //保存当前版本
        [[NSUserDefaults standardUserDefaults] setDouble:currentVersion forKey:@"sandBoxVersion"];
        
        return YES;
    }
    
    return NO;
}

#pragma mark - 控制器切换方法
- (void)switchController:(NSNotification *)notice
{
//    ZDLog(@"%@", notice.userInfo);
    
    self.window.rootViewController = [[(Class)notice.userInfo[@"class"] alloc] init];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
